//
//  EuropeViewController.m
//  BeanTrip
//
//  Created by dlios on 15-7-1.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "EuropeViewController.h"
#import "EuropeListModel.h"
#import "EuropeCollectionViewCell.h"
#import "HTTPTool.h"
#import "UIImageView+WebCache.h"
#import "MBProgressHUD.h"

#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGH ([UIScreen mainScreen].bounds.size.height)

#import "DetailViewController.h"
@interface EuropeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
/**
 *  设置一个小菊花属性
 */
@property (nonatomic, retain) MBProgressHUD *HUD;
/**
 *  设置一个提示框属性
 */
@property(nonatomic, retain)UIAlertView *alter;
@end

@implementation EuropeViewController
/**
 *  内存处理
 */
- (void)dealloc
{
    [_EuropeListArr release];
    [_titleStr release];
    [_HUD release];
    [_alter release];
    [_titleStr release];
    [_dataHandel release];
    [super dealloc];
}
/**
 *  初始化数组
 *
 *  @param nibNameOrNil   nibNameOrNil description
 *  @param nibBundleOrNil nibBundleOrNil description
 *
 *  @return return value description
 */
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.EuropeListArr=[NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];
    /**
     导航栏上显示文字位置
     
     :returns:
     */
    UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    /**
     *  把传过来的title值赋给mylabel
     */
    mylabel.text = self.titleStr;
    /**
     *  设置导航栏文字的颜色
     */
    mylabel.textColor = [UIColor whiteColor];
    /**
     *  文字的字体
     */
    mylabel.font = [UIFont systemFontOfSize:17];
    self.navigationItem.titleView = mylabel;
    
    /**
     1.创建瀑布流效果
     */
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    /**
     2.每一个item的大小
     */
    flowLayout.itemSize=CGSizeMake((WIDTH-30)/2, (HEIGH-64)/4);
    /**
     *3.设置最小的行间距
     */
    flowLayout.minimumLineSpacing = 10;
    /**
     *4.设置最小的列间距
     */
    // flowLayout.minimumInteritemSpacing = 0;
    /**
     *5.滑动的方向(垂直滑动),默认是垂直滑动
     */
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    /**
     *6.边缘
     */
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    /**
     *7.创建瀑布流视图
     */
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0, WIDTH, HEIGH-64-49) collectionViewLayout:flowLayout];
    /**
     *8.签订两个协议,一个dataSource,一个delegate
     */
    collectionView.delegate = self;
    collectionView.dataSource = self;
    /**
     *9.把瀑布流视图添加到view上
     */
    [self.view addSubview:collectionView];
    [collectionView release];
    /**
     *设置瀑布流的背景颜色
     */
    collectionView.backgroundColor=[UIColor whiteColor];
    /**
     10.注册
     *第一个参数:需要指定注册的类型;第二个参数:重用标志
     */
    [collectionView registerClass:[EuropeCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    collectionView.tag=1002;
   
    
    
    
    
    
    /**
     菊花
     
     :returns: return value description
     */
    self.HUD = [[MBProgressHUD alloc] initWithView:collectionView];
    [collectionView addSubview:self.HUD];
    self.HUD.labelText = @"加载中";
    [self.HUD show:YES];
    
   
    /**
     *执行createData方法
     */

    [self createData];
    
  
}





-(void)createData
{
    [HTTPTool get:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%ld/",(long)self.index] body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSArray *dataArr=dic[@"data"];
        for (NSDictionary *temp in dataArr) {
        EuropeListModel *cover = [[ EuropeListModel alloc] initWithDic:temp];
            [self.EuropeListArr addObject:cover];
                }
        
        /**
         *  根据tag值取出collection
         *
         *  @param UICollectionView
         *
         *  @return
         */
    UICollectionView *collectionView = (UICollectionView *)[self.view viewWithTag:1002];
        /**
         *  数据刷新
         */
        [collectionView reloadData];
     //   NSLog(@"%@",self.EuropeListArr);
        
        /**
         *加载完成,取消菊花
         */
        [self.HUD removeFromSuperview];
    }
     failure:^(NSError *error) {
        
         [self.HUD removeFromSuperview];
         self.alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
         [self.alter show];
         [self.alter release];
          NSLog(@"%@",error);
    }];


}

/**
 *23.collectionView里numberOfItemsInSection(必须要实现的方法)
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.EuropeListArr.count;

}

/**
 *24.对于每个cell进行操作(必须要实现的方法)
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
   EuropeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];

    /**
     *创建model对象
     */
    EuropeListModel *coverPic = self.EuropeListArr[indexPath.row];

    cell.myEuropeLabel.text=coverPic.name;
    /**
     *用sdwebimage对imageview进行图片赋值
     */
    [cell.myEuropeImageView sd_setImageWithURL:[NSURL URLWithString:coverPic.cover]];
    
    /**
     *  占位图
     */
    NSURL *url = [NSURL URLWithString:coverPic.cover];
    UIImage *image = [UIImage imageNamed:@"BeanTrip.png"];
    [cell.myEuropeImageView sd_setImageWithURL:url placeholderImage:image];
    
    
    /**
     *  动画效果
     */
//    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
//    [UIView animateWithDuration:0.5 animations:^{
//        
//        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
//    }];
    
    
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];
    
    
    
    
     return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail = [[DetailViewController alloc] init];
    detail.hidesBottomBarWhenPushed = YES;
    
        EuropeListModel *EuropeDetail=self.EuropeListArr[indexPath.row];
        detail.idString=EuropeDetail.bId;
        detail.typeNumber=EuropeDetail.type;
        detail.getTitle = self.titleStr;
       [self.navigationController pushViewController:detail animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
